﻿using System;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace ClownStopper
{
    public class UnregisteredClient : IClient
    {
        #region iClient Members

        public MessageClientDetails Client { get; set; }


        public String Group { get; set; }

        public /*MessageType*/ int Type
        {
            get { return (int) MessageType.UnregisterMessage; }
        }

        public Byte[] GetMessagePacket()
        {
            String textResult = "";
            textResult += "<message>";

            textResult += "<type>" + Type + "</type>";
            textResult += "<group>" + Group + "</group>";
            textResult += "<clientdetails><name>" + Client.ClientName + "</name><ipaddress>" + Client.ClientIPAddress + "</ipaddress><listenport>" + Client.ClientListenPort + "</listenport></clientdetails>";

            textResult += "</message>";

            return Encoding.UTF8.GetBytes(textResult);
        }

        public bool Parse(Byte[] data, out string error)
        {
            error = null;
            //parse the message from "incoming data packet"
            String messageContent = Encoding.UTF8.GetString(data);
            //////////////////////////////////////////////////////////////////////////
            //Data validation
            var xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.LoadXml(messageContent);
            }
            catch (XmlException ex)
            {
                error = "There was an xml parsing error in the received message : " + ex.Message;
                return false;
            }
            catch (Exception ex)
            {
                error = "There was an error in the received message : " + ex.Message;
                return false;
            }

            MessageType type = MessageType.EmptyMessage;

            XmlElement messageElement = xmlDoc.DocumentElement;
            if (messageElement != null)
                if (messageElement.Name == "message")
                {
                    foreach (XmlNode node in messageElement.ChildNodes)
                    {
                        if (node.Name == "type")
                        {
                            type = (MessageType) Enum.Parse(typeof (MessageType), node.InnerText);
                            break;
                        }
                    }
                }

            if (type != MessageType.UnregisterMessage)
            {
                error = "UnregisterClient: The supplied data was the wrong message type!";
                return false;
            }

            //////////////////////////////////////////////////////////////////////////
            // The real data parsing
            Group = "";
            Client = null;
            Client = new MessageClientDetails();

            foreach (XmlNode node in messageElement.ChildNodes)
            {
                if (node.Name == "group")
                {
                    Group = node.InnerText;
                }
                else if (node.Name == "clientdetails")
                {
                    foreach (XmlNode detailsNode in node.ChildNodes)
                    {
                        if (detailsNode.Name == "name")
                        {
                            Client.ClientName = detailsNode.InnerText;
                        }
                        else if (detailsNode.Name == "ipaddress")
                        {
                            Client.ClientIPAddress = detailsNode.InnerText;
                        }
                        else if (detailsNode.Name == "listenport")
                        {
                            Client.ClientListenPort = int.Parse(detailsNode.InnerText);
                        }
                    }
                }
            }

            //////////////////////////////////////////////////////////////////////////
            return true;
        }

        public IMessage Clone()
        {
            var result = new UnregisteredClient();
            result.Group = Group;
            result.Client = new MessageClientDetails();
            result.Client.ClientIPAddress = Client.ClientIPAddress;
            result.Client.ClientListenPort = Client.ClientListenPort;
            return result;
        }

        #endregion
    }
}